<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['putStorage:halfStart:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['putStorage:halfStart:edit']">
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['putStorage:halfStart:del']">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleSubmit"
          v-hasPermi="['putStorage:halfStart:submit']">
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['putStorage:halfStart:export']">
          导出
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :data="RpmMstrList"
      @row-click="clickRows"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column label="入库单号" :show-overflow-tooltip="true" width="135" align="center" prop="rpmDjbh" />
      <el-table-column label="入库日期" :show-overflow-tooltip="true" align="center" prop="rpmRkrq" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rpmRkrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库类型" :show-overflow-tooltip="true" width="100" align="center" prop="rpmType">
        <template #default="scope">
          <dict-tag :options="rpmTypeOptions" :value="scope.row.rpmType" />
        </template>
      </el-table-column>
      <el-table-column label="入库仓库" :show-overflow-tooltip="true" width="120" align="center" prop="rpmCkdm">
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.rpmCkdm" />
        </template>
      </el-table-column>
      <el-table-column label="班次" :show-overflow-tooltip="true" width="90" align="center" prop="rpmBcdm" />
      <el-table-column label="客户代码" :show-overflow-tooltip="true" width="135" align="center" prop="rpmKhdm">
        <template #default="scope">
          <dict-tag :options="cus_list" :value="scope.row.rpmKhdm" />
        </template>
      </el-table-column>
      <el-table-column label="开单人员" :show-overflow-tooltip="true" width="100" align="center" prop="rpmJlry" />
      <el-table-column label="检验结果" :show-overflow-tooltip="true" width="100" align="center" prop="rpmJyjg" />
      <el-table-column label="备注" :show-overflow-tooltip="true" width="120" align="center" prop="rpmDesc" />
      <el-table-column label="开单日期" :show-overflow-tooltip="true" align="center" prop="rpmJlrq" width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rpmJlrq, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检验人员" :show-overflow-tooltip="true" width="100" align="center" prop="rpmJyry" />
      <el-table-column label="检验日期" :show-overflow-tooltip="true" align="center" prop="rpmJyrq" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rpmJyrq, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过账人员" :show-overflow-tooltip="true" width="100" align="center" prop="rpmGzry" />
      <el-table-column label="过账日期" :show-overflow-tooltip="true" align="center" prop="rpmGzrq" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.rpmGzrq, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="100" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAddFull(scope.row)"
            :disabled="scope.row.rpmZtbz != '1' || scope.row.rpmTjflag != '0'"
            v-hasPermi="['putStorage:halfStart:edit']">
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加子项对话框   -->
    <el-dialog v-model="openChildren" :fullscreen="true" :close-on-click-modal="false" append-to-body>
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-form-item label="制造单号" v-if="addDate" prop="rpdZzdh">
            <el-input
              style="width: 200px"
              v-model="queryChildParams.rpdZzdh"
              placeholder="请输入制造单号"
              clearable
              size="small"
              @keyup.enter.native="serachChildrened" />
          </el-form-item>
          <el-form-item label="物料代码" prop="rpdWldm">
            <el-input
              style="width: 200px"
              v-model="queryChildParams.rpdWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="serachChildrened" />
          </el-form-item>
          <el-form-item label="规则描述" prop="rpdWlgg">
            <el-input
              style="width: 200px"
              v-model="queryChildParams.rpdWlgg"
              placeholder="请输入规则描述"
              clearable
              size="small"
              @keyup.enter.native="serachChildrened" />
          </el-form-item>
          <el-form-item label="机台编号" v-if="addDate" prop="rpdJtbh">
            <el-input
              style="width: 200px"
              v-model="queryChildParams.rpdJtbh"
              placeholder="请输入机台编号"
              clearable
              size="small"
              @keyup.enter.native="serachChildrened" />
          </el-form-item>
          <el-form-item label="入库类型" v-if="addDate" prop="rpdType">
            <el-select style="width: 200px" v-model="queryChildParams.rpdType" size="small">
              <el-option v-for="item in rpdTypeOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="serachChildrened">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <!--      @row-click="clickdel"-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="serachAddList"
          border
          stripe
          :height="tableHData"
          ref="somCheckTable"
          @scrollBottom="load2"
          @row-click="tableAddClick"
          :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" min-width="5%" type="index" align="center" />
          <!-- <el-table-column type="selection" min-width="5%" align="center"/> -->
          <el-table-column label="客户编号" min-width="12%" :show-overflow-tooltip="true" align="center" prop="itm_custwldm" />
          <el-table-column label="制造单号" min-width="15%" :show-overflow-tooltip="true" align="center" prop="moe_zzdh" />
          <el-table-column label="颜色" min-width="10%" :show-overflow-tooltip="true" align="center" prop="itm_ysdm" />
          <el-table-column label="物料代码" min-width="15%" :show-overflow-tooltip="true" align="center" prop="rpdWldm" />
          <el-table-column label="品名规格" min-width="35%" :show-overflow-tooltip="true" prop="itm_pmgg" />
          <el-table-column label="单位" min-width="8%" :show-overflow-tooltip="true" align="center" prop="itm_dw">
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itm_dw" />
            </template>
          </el-table-column>
          <el-table-column label="机台" min-width="8%" :show-overflow-tooltip="true" align="center" prop="moe_jtbh" />
          <el-table-column label="计划数量" min-width="13%" :show-overflow-tooltip="true" v-if="addDate" align="center" prop="moe_scsl" />
          <el-table-column label="良品数量" min-width="13%" :show-overflow-tooltip="true" v-if="addDate" align="center" prop="moe_lpsl" />
          <el-table-column label="领料配套数量" min-width="13%" :show-overflow-tooltip="true" v-if="addDate" align="center" prop="rpd_sqsl" />
          <el-table-column label="已申请数量" min-width="13%" :show-overflow-tooltip="true" align="center" prop="v_sqsl" />
          <el-table-column label="待申请数量" min-width="13%" :show-overflow-tooltip="true" v-if="addDate" align="center" prop="v_dsqs" />
          <el-table-column label="需申请数量" min-width="13%" :show-overflow-tooltip="true" prop="rpdSqsl" align="center">
            <template #default="scope">
              <el-input class="inputName" type="number" @input="changValue(scope.row)" v-model="scope.row.rpdSqsl"></el-input>
            </template>
          </el-table-column>
        </el-table>
        <div style="position: absolute; right: 70px; font-size: 16px; font-weight: 700; padding: 8px">合计： {{ this.countSqsl }}</div>
        <el-tabs v-model="activeTab">
          <el-tab-pane label="入库明细" name="first">
            <el-table border stripe :height="tableHChild" :data="rwdDetList" :header-cell-style="{ 'text-align': 'center' }">
              <el-table-column label="#" type="index" width="40" align="center" fixed="left" />
              <el-table-column label="客户编号" width="135" align="center" prop="rpdCusWldm" fixed="left" />
              <el-table-column label="制造单号" width="135" align="center" prop="rpdZzdh" />
              <el-table-column label="机台" width="135" align="center" prop="rpdJtbh" />
              <el-table-column label="物料代码" width="135" align="center" prop="rpdWldm" />
              <el-table-column label="产品名称" width="135" align="center" prop="rpdWlpm" />
              <el-table-column label="产品规格" width="135" align="center" prop="rpdWlgg" />
              <el-table-column label="单位" :show-overflow-tooltip="true" width="60" align="center" prop="rpdDw">
                <template #default="scope">
                  <dict-tag :options="unitFlagOptions" :value="scope.row.rpdDw" />
                </template>
              </el-table-column>
              <el-table-column label="申请数量" width="80" align="center" prop="rpdSqsl" />
              <el-table-column label="允收数量" width="80" align="center" prop="rpdOksl" />
              <el-table-column label="拒收数量" width="80" align="center" prop="rpdNgsl" />
              <el-table-column label="入库数量" width="80" align="center" prop="rpdRksl" />
              <el-table-column label="箱包装数" width="80" align="center" prop="rpdMxsl" />
              <el-table-column label="检验人员" width="80" align="center" prop="rpdJyry" />
              <el-table-column label="检验日期" width="80" align="center" prop="rpdJyrq" />
              <el-table-column label="备注" width="120" align="center" prop="rpdDesc" />
              <el-table-column label="检验结果" width="120" align="center" prop="rpdJyjg" />
              <el-table-column label="结果描述" width="120" align="center" prop="rpdJyms" />
              <el-table-column label="入库人员" :show-overflow-tooltip="true" width="100" align="center" prop="rpdRkry" />
              <el-table-column label="入库日期" :show-overflow-tooltip="true" align="center" prop="rpdRkrq" width="150">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.rpdRkrq, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" align="center" width="100" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-button size="mini" type="text" :disabled="single" @click="handleCdel(scope.row)" v-hasPermi="['putStorage:halfStart:edit']">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="入库统计" name="second">
            <el-table border :data="rwdDetTotal" stripe :height="tableHChild">
              <el-table-column label="#" width="40" align="center" />
              <el-table-column label="客户编号" show-overflow-tooltip align="center" prop="rpdCusWldm" />
              <el-table-column label="物料代码" show-overflow-tooltip align="center" prop="rpdWldm" />
              <el-table-column label="产品名称" show-overflow-tooltip align="center" prop="rpdWlpm" />
              <el-table-column label="产品规格" show-overflow-tooltip align="center" prop="rpdWlgg" />
              <el-table-column label="客户编码" show-overflow-tooltip align="center" prop="rpdCusWldm" />
              <el-table-column label="单位" :show-overflow-tooltip="true" width="60" align="center" prop="rpdDw">
                <template #default="scope">
                  <dict-tag :options="unitFlagOptions" :value="scope.row.rpdDw" />
                </template>
              </el-table-column>
              <el-table-column label="最小包装" align="center" prop="itmMinbz" />
              <el-table-column label="包装数量" align="center" prop="rpdMxsl" />
              <el-table-column label="申请数量" align="center" prop="rpdSqsl" />
              <el-table-column label="允收数量" align="center" prop="rpdOksl" />
              <el-table-column label="拒收数量" align="center" prop="rpdNgsl" />
              <el-table-column label="入库数量" align="center" prop="rpdRksl" />
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="产品领用Bom" name="three">
            <ListProductBom :cldZzdh="cldZzdh" :itmCkdm="queryChildParams.rpdCkdm"></ListProductBom>
          </el-tab-pane>
        </el-tabs>

        <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
          <el-button @click="openChildren = false">取消</el-button>
          <el-button type="primary" @click="handAddChildren">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--子表Tab-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="入库明细" name="first">
        <el-table border stripe :height="tableHChild" :data="rwdDetList" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="40" align="center" fixed="left" />
          <el-table-column label="客户编号" width="135" align="center" prop="rpdCusWldm" fixed="left" />
          <el-table-column label="制造单号" width="135" align="center" prop="rpdZzdh" />
          <el-table-column label="机台" width="135" align="center" prop="rpdJtbh" />
          <el-table-column label="物料代码" width="135" align="center" prop="rpdWldm" />
          <el-table-column label="产品名称" width="135" align="center" prop="rpdWlpm" />
          <el-table-column label="产品规格" width="135" align="center" prop="rpdWlgg" />
          <el-table-column label="单位" :show-overflow-tooltip="true" width="60" align="center" prop="rpdDw">
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.rpdDw" />
            </template>
          </el-table-column>
          <el-table-column label="申请数量" width="80" align="center" prop="rpdSqsl" />
          <el-table-column label="允收数量" width="80" align="center" prop="rpdOksl" />
          <el-table-column label="拒收数量" width="80" align="center" prop="rpdNgsl" />
          <el-table-column label="入库数量" width="80" align="center" prop="rpdRksl" />
          <el-table-column label="箱包装数" width="80" align="center" prop="rpdMxsl" />
          <el-table-column label="检验人员" width="80" align="center" prop="rpdJyry" />
          <el-table-column label="检验日期" width="80" align="center" prop="rpdJyrq" />
          <el-table-column label="备注" width="120" align="center" prop="rpdDesc" />
          <el-table-column label="检验结果" width="120" align="center" prop="rpdJyjg" />
          <el-table-column label="结果描述" width="120" align="center" prop="rpdJyms" />
          <el-table-column label="入库人员" :show-overflow-tooltip="true" width="100" align="center" prop="rpdRkry" />
          <el-table-column label="入库日期" :show-overflow-tooltip="true" align="center" prop="rpdRkrq" width="150">
            <template #default="scope">
              <span>{{ parseTime(scope.row.rpdRkrq, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="箱号" width="120" align="center" prop="rrqXs" />
          <el-table-column label="生产日期" :show-overflow-tooltip="true" align="center" prop="rrqRq" width="150">
            <template #default="scope">
              <span>{{ parseTime(scope.row.rrqRq, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
          <el-table-column fixed="right" label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button size="mini" type="text" :disabled="single" @click="handleCdel(scope.row)" v-hasPermi="['purchase:qgmMstr:remove']">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="入库统计" name="second">
        <el-table border stripe :data="rwdDetTotal" :height="tableHChild">
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column label="客户编号" show-overflow-tooltip align="center" prop="rpdCusWldm" />
          <el-table-column label="物料代码" show-overflow-tooltip align="center" prop="rpdWldm" />
          <el-table-column label="产品名称" show-overflow-tooltip align="center" prop="rpdWlpm" />
          <el-table-column label="产品规格" show-overflow-tooltip align="center" prop="rpdWlgg" />
          <el-table-column label="客户编码" show-overflow-tooltip align="center" prop="rpdCusWldm" />
          <el-table-column label="单位" :show-overflow-tooltip="true" width="60" align="center" prop="rpdDw">
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.rpdDw" />
            </template>
          </el-table-column>
          <el-table-column label="最小包装" align="center" prop="itmMinbz" />
          <el-table-column label="包装数量" align="center" prop="rpdMxsl" />
          <el-table-column label="申请数量" align="center" prop="rpdSqsl" />
          <el-table-column label="允收数量" align="center" prop="rpdOksl" />
          <el-table-column label="拒收数量" align="center" prop="rpdNgsl" />
          <el-table-column label="入库数量" align="center" prop="rpdRksl" />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="720px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
        <el-col :span="12">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="dateRange"
              :picker-options="pickerOptions"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库类型" prop="rpmType">
            <el-select v-model="queryParams.rpmType" class="input240" placeholder="请选择入库类型" clearable size="small">
              <el-option v-for="dict in rpmTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库仓库" prop="rpmCkdm">
            <el-select v-model="queryParams.rpmCkdm" style="width: 240px" clearable filterable placeholder="请选择仓库">
              <el-option v-for="item in storageOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库单号" prop="rpmDjbh">
            <el-input
              v-model="queryParams.rpmDjbh"
              placeholder="请输入入库单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="综合查询" prop="integrated">
            <el-input
              v-model="queryParams.integrated"
              placeholder="输入物料代码或品名规格"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-form-item prop="rwmZtbz">
            <el-radio-group v-model="queryParams.rpmZtbz">
              <el-radio style="margin-right: 10px" v-for="dict in ZtbzOptions" :key="dict.dictValue" :label="dict.dictValue">
                {{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改成品/半成品入库对话框 -->
    <el-dialog :title="title" v-model="open" :close-on-click-modal="false" draggable width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="入库单号" prop="rpmDjbh">
            <el-input v-model="form.rpmDjbh" placeholder="流水号生成" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库日期" prop="rpmRkrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.rpmRkrq"
              type="date"
              style="width: 240px"
              placeholder="选择入库日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库类型" prop="rpmType">
            <el-select v-model="form.rpmType" style="width: 240px" filterable placeholder="请选择入库类型">
              <el-option
                v-for="dict in rpmTypeOptions"
                :key="dict.dictValue"
                :label="'[' + dict.dictValue + ']' + dict.dictLabel"
                :value="dict.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库仓库" prop="rpmCkdm">
            <el-select v-model="form.rpmCkdm" class="input240" filterable placeholder="请选择入库仓库">
              <el-option v-for="dict in storageOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户代码" prop="rpmKhdm">
            <el-select v-model="form.rpmKhdm" style="width: 560px" :disabled="title == '修改成品/半成品入库'" filterable placeholder="请选择客户代码">
              <el-option v-for="dict in cus_list" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="rpmDesc">
            <el-input type="textarea" v-model="form.rpmDesc" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRpmMstr,
  getRpmMstr,
  delRpmMstr,
  addRpmMstr,
  updateRpmMstr,
  exportRpmMstr,
  serachDateList,
  listRpd,
  saveDate,
  submitRpm,
  delChildren,
} from '@/api/putStorage/RpmMstr'
import { getAllCustomer, getAllCustomerTwo } from '@/api/project/customer'
import { getstorageType, getAll } from '@/api/repertory/Storage'
import { listBcm } from '@/api/base/bcm'
import ListProductBom from '@/views/output/outputData/picking/ListProductBom.vue'

export default {
  name: 'HalfStart',
  components: { ListProductBom },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeTab: 'first',
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      cldZzdh: '',
      tableH: (document.body.clientHeight - 130) / 2,
      tableHData: (document.body.clientHeight - 270) / 2,

      tableHChild: (document.body.clientHeight - 280) / 2,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 成品/半成品入库表格数据
      RpmMstrList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openChildren: false,
      addDate: true,
      // 入库类型字典
      rpmTypeOptions: [],
      rwdDetTotal: [],
      rwdDetList: [],
      // 有效客户代码字典
      customerOptions: [],
      cus_list: [
        { dictValue: '.H', dictLabel: '海尔' },
        { dictValue: '.M', dictLabel: '美的' },
        { dictLabel: '所有客户', dictValue: 'SY', listClass: 'default' },
      ],
      // 所有客户代码字典
      allCustomerOptions: [],
      //有效仓库字典
      storageOptions: [],
      //所有仓库字典
      allStorageOptions: [],
      // 入库类型
      rpdTypeOptions: [
        {
          dictLabel: '第一仓入库',
          dictValue: 'A',
        },
        {
          dictLabel: '第二仓入库',
          dictValue: 'C',
        },
      ],
      //请求状态
      ZtbzOptions: [
        {
          dictLabel: '未检验',
          dictValue: '1',
        },
        {
          dictLabel: '已检验',
          dictValue: '2',
        },
        {
          dictLabel: '全部',
          dictValue: '',
        },
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
      // 开单日期时间范围
      dateRange: [new Date(new Date().getTime() - 3600 * 1000 * 24 * 30), new Date(new Date().getTime() + 3600 * 1000 * 24 * 60)],
      serachAddList: [],
      unitFlagOptions: [],
      selectioned: [],
      rowData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        rpmDjbh: null,
        rpmType: null,
        integrated: '',
        rpmZtbz: '1',
      },
      // 查询子项参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 20,
        //入库类型
        rpdType: null,
        //入库仓库
        rpdCkdm: null,
        //客户代码
        rpdKhdm: null,
        //订单编号
        rpdDjbh: null,
        //制造单号
        rpdZzdh: null,
        //机台编号
        rpdJtbh: null,
        //物料代码
        rpdWldm: null,
        //规格描述
        rpdWlgg: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rpmRkrq: [{ required: true, message: '入库日期不能为空', trigger: 'blur' }],
        rpmType: [{ required: true, message: '入库类型不能为空', trigger: 'change' }],
        rpmCkdm: [{ required: true, message: '入库仓库不能为空', trigger: 'blur' }],
        rpmKhdm: [{ required: true, message: '客户代码不能为空', trigger: 'change' }],
      },
    }
  },
  created() {
    //获取入库类型字典
    this.getDicts('input_StorageType').then((response) => {
      this.rpmTypeOptions = response.data
    })
    //单位
    this.getDicts('sys_unit').then((response) => {
      this.unitFlagOptions = response.data
    })
    //获取有效客户
    getAllCustomer().then((response) => {
      this.customerOptions.push({ dictLabel: '[SY]所有', dictValue: 'SY', listClass: 'default' })
      for (let i of response.data) {
        i.dictLabel = '[' + i.cusCode + ']' + i.cusName
        i.dictValue = i.cusCode
        i.listClass = 'default'
        this.customerOptions.push(i)
      } //this.customerOptions = response.data
    })
    //获取所有客户
    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = '[' + i.cusCode + '] ' + i.cusName
        i.dictValue = i.cusCode
        i.listClass = 'default'
      }

      this.allCustomerOptions = response.data
      this.allCustomerOptions.push({ dictLabel: '[SY]所有', dictValue: 'SY', listClass: 'default' })
    })

    //根据状态获取存储仓库
    getstorageType('0').then((response) => {
      this.storageOptions = response.data.filter((o) => {
        return o.storageCode.indexOf('CP') > -1 || o.storageCode.indexOf('BC') > -1
      })

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName
        item.listClass = 'default'
        item.dictValue = item.storageId + ''
      }
    })

    //根据状态获取存储仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName
        item.listClass = 'default'
        item.dictValue = item.storageId + ''
      }
    })

    this.getList()
  },
  computed: {
    countSqsl() {
      return this.serachAddList
        .filter((a) => {
          return !!a.rpdSqsl && a.rpdSqsl > 0
        })
        .reduce((pre, cur) => pre + parseFloat(cur.rpdSqsl), 0)
    },
  },
  methods: {
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.rpmZtbz == '1' && row.rpmTjflag == '1') {
        return 'padding:0px 0;color:#0fac09'
      }
      //通过
      else if (row.rpmZtbz == '2' && row.rpmTjflag == '1') {
        return 'padding:0px 0;color:#0323af'
      }
      //检验
      else if (row.rpmZtbz == '6' && row.rpmTjflag == '1') {
        return 'padding:0px 0;color:#bb0331'
      }
      if (row.rpmZtbz == '2' && row.rpmTjflag == '1') {
        return 'padding:0px 0;color:#bb0331'
      }
    },

    handleAddFull(row) {
      if (row.rpmType == 'B') {
        this.addDate = false
      } else {
        this.addDate = true
      }
      this.queryChildParams.rpdType = row.rpmType
      this.queryChildParams.rpdDjbh = row.rpmDjbh
      this.queryChildParams.rpdCkdm = row.rpmCkdm
      this.queryChildParams.rpdKhdm = row.rpmKhdm
      this.openChildren = true

      // this.serachChildrened()
    },
    handAddChildren() {
      const datas = this.serachAddList
        .filter((a) => {
          return !!a.rpdSqsl && a.rpdSqsl > 0
        })
        .map((i) => {
          let data = {
            rpdDjbh: i.rpdDjbh,
            rpdWldm: i.rpdWldm,
            rpdMoeid: i.moeId,
            rpdSqsl: i.rpdSqsl,
            rpdYsqs: i.v_sqsl,
            rpdZzdh: i.moe_zzdh,
            rpdLpsl: i.moe_lpsl,
            rpdDdsl: i.moe_scsl,
          }
          if (i.moeId != null && i.moeId != '') {
            data.rpdType = 'A'
          }
          return data
        })
      saveDate(datas).then((res) => {
        let msg = res.msg
        if (msg === '添加成功') {
          this.msgSuccess(msg)
          this.serachChildrened()
          this.getRwdDetList(this.rowData.rpmDjbh)
        } else if (msg === '添加失败') {
          this.msgError(msg)
        } else {
          this.msgInfo(msg)
        }
      })
    },
    clickRows(row) {
      //获得选中的详细数据
      this.rowData = row
      //获得当前单据的状态
      this.single = row == null || row.rpmZtbz != '1' || row.rpmTjflag != '0'
      //获得选中的详细数据
      this.getRwdDetList(row.rpmDjbh)
    },
    /** 获取RwdDet 数据*/
    getRwdDetList(rpdDjbh) {
      this.rwdDetList = []
      this.rwdDetTotal = []
      if (rpdDjbh != null && rpdDjbh != '') {
        listRpd(rpdDjbh).then((response) => {
          this.rwdDetList = response.data.detail
          this.rwdDetTotal = response.data.total
        })
      }
    },
    /** 查询成品/半成品入库列表 */
    getList(row) {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.params['beginRpmJlrq'] = this.moment(this.dateRange[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.params['endRpmJlrq'] = this.moment(this.dateRange[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      this.queryParams.pageNum = 1
      listRpmMstr(this.queryParams).then((response) => {
        this.RpmMstrList = []
        this.rwdDetList = []
        this.rwdDetTotal = []
        this.RpmMstrList = this.RpmMstrList.concat(response.rows)
        this.total = response.total
        this.loading = false
        if (row != null) {
          this.clickRows(row)
        } else if (this.RpmMstrList != null && this.RpmMstrList.length > 0) {
          this.clickRows(this.RpmMstrList[0])
        }
      })
    },
    tableAddClick(row) {
      this.cldZzdh = row.moe_zzdh
    },
    //load
    load() {
      if (this.queryParams.pageNum < Math.ceil(this.total / this.queryParams.pageSize)) {
        this.queryParams.pageNum++
        listRpmMstr(this.queryParams).then((response) => {
          this.RpmMstrList = this.RpmMstrList.concat(response.rows)
          this.total = response.total
          this.loading = false
        })
      }
      return 0
    },
    /** 查询可接收的物料 */
    serachChildrened() {
      this.queryChildParams.pageNum = 1
      serachDateList(this.queryChildParams).then((response) => {
        this.serachAddList = response.rows
        this.total2 = response.total
      })
      this.getRwdDetList(this.rowData.rpmDjbh)
    },
    load2() {
      if (this.queryChildParams.pageNum < Math.ceil(this.total2 / this.queryChildParams.pageSize)) {
        this.queryChildParams.pageNum++
        serachDateList(this.queryChildParams).then((response) => {
          this.serachAddList = this.serachAddList.concat(response.rows)
          this.total2 = response.total
        })
      }
      return 0
    },
    changValue(row) {
      if (row.rpdSqsl > row.v_dsqs || row.rpdSqsl < 0) {
        // this.msgError('不允许超过待申请数')
        row.rpdSqsl = row.v_dsqs
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        rpmId: null,
        rpmDjbh: null,
        rpmRkrq: new Date(),
        rpmType: null,
        rpmCkdm: null,
        rpmBcdm: null,
        rpmKhdm: null,
        rpmJyjg: null,
        rpmDesc: null,
        rpmTjflag: null,
        rpmNgdesc: null,
        rpmZtbz: null,
        rpmJlry: null,
        rpmJlrq: null,
        rpmJyry: null,
        rpmJyrq: null,
        rpmGzry: null,
        rpmGzrq: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.RpmMstrList = []
      this.getList()
      this.openSearch = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加成品/半成品入库'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const rpmId = this.rowData.rpmId
      getRpmMstr(rpmId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改成品/半成品入库'
      })
    },
    handleSearch() {
      this.reset()
      this.title = '查询成品/半成品入库'
      this.openSearch = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.rpmId != null) {
            updateRpmMstr(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRpmMstr(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rpmIds = this.rowData.rpmId
      this.$confirm('是否确认删除成品/半成品入库编号为"' + rpmIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delRpmMstr(rpmIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 提交 **/
    handleSubmit() {
      const rpmId = this.rowData.rpmId
      submitRpm(rpmId)
        .then((res) => {
          this.getList()
          this.msgSuccess('提交成功')
        })
        .catch(() => {
          this.msgError('提交失败')
        })
    },
    /** 删除子项 */
    handleCdel(row) {
      delChildren(row.rpdId)
        .then((res) => {
          this.serachChildrened()
          this.getRwdDetList(row.rpdDjbh)
          this.msgSuccess('删除成功')
        })
        .catch(() => {
          this.msgError('删除失败')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有成品/半成品入库数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportRpmMstr(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>

<style lang="scss" scoped></style>
